﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using ReadyApps.Wiki;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ReadyAppsTest.Models.Wiki
{
    /// <summary>
    /// Summary description for WikiPageLinkTest
    /// </summary>
    [TestClass]
    public class WikiPageLinkTest
    {
        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Constructor Tests

        [TestMethod]
        [Description("Tests the WikiPageLink constructor with valid options")]
        public void WikiPageLinkConstructorTest()
        {
            int pageLinkKey = 2;
            string displayText = "display";
            string linkUrl = "url";
            string target = null;
            WikiPageLink pageLinkTarget = new WikiPageLink(pageLinkKey, displayText, linkUrl, target);
            PrivateObject targetWrapper = new PrivateObject(pageLinkTarget);
            Assert.AreEqual(pageLinkKey, (int)targetWrapper.GetProperty("PageLinkKey"));
            Assert.AreEqual(displayText, (string)targetWrapper.GetProperty("DisplayText"));
            Assert.AreEqual(linkUrl, (string)targetWrapper.GetProperty("LinkUrl"));
            Assert.AreEqual(string.Empty, (string)targetWrapper.GetProperty("Target"));

            target = "target";
            pageLinkTarget = new WikiPageLink(pageLinkKey, displayText, linkUrl, target);
            targetWrapper = new PrivateObject(pageLinkTarget);
            Assert.AreEqual(pageLinkKey, (int)targetWrapper.GetProperty("PageLinkKey"));
            Assert.AreEqual(displayText, (string)targetWrapper.GetProperty("DisplayText"));
            Assert.AreEqual(linkUrl, (string)targetWrapper.GetProperty("LinkUrl"));
            Assert.AreEqual(target, (string)targetWrapper.GetProperty("Target"));
        }

        [TestMethod]
        [Description("Tests the failure of the WikiPageLink constructor when given a null displayText parameter")]
        [ExpectedException(typeof(ArgumentNullException))]
        public void WikiPageLinkConstructorDisplayTextNullTest()
        {
            int pageLinkKey = 2;
            string displayText = null;
            string linkUrl = "url";
            string target = null;
            WikiPageLink pageLinkTarget = new WikiPageLink(pageLinkKey, displayText, linkUrl, target);
        }

        [TestMethod]
        [Description("Tests the failure of the WikiPageLink constructor when given an empty displayText parameter")]
        [ExpectedException(typeof(ArgumentNullException))]
        public void WikiPageLinkConstructorDisplayTextEmptyTest()
        {
            int pageLinkKey = 2;
            string displayText = string.Empty;
            string linkUrl = "url";
            string target = null;
            WikiPageLink pageLinkTarget = new WikiPageLink(pageLinkKey, displayText, linkUrl, target);
        }

        [TestMethod]
        [Description("Tests the failure of the WikiPageLink constructor when given a null linkUrl parameter")]
        [ExpectedException(typeof(ArgumentNullException))]
        public void WikiPageLinkConstructorLinkUrlNullTest()
        {
            int pageLinkKey = 2;
            string displayText = "display";
            string linkUrl = null;
            string target = null;
            WikiPageLink pageLinkTarget = new WikiPageLink(pageLinkKey, displayText, linkUrl, target);
        }

        [TestMethod]
        [Description("Tests the failure of the WikiPageLink constructor when given an empty linkUrl parameter")]
        [ExpectedException(typeof(ArgumentNullException))]
        public void WikiPageLinkConstructorLinkUrlEmptyTest()
        {
            int pageLinkKey = 2;
            string displayText = "display";
            string linkUrl = string.Empty;
            string target = null;
            WikiPageLink pageLinkTarget = new WikiPageLink(pageLinkKey, displayText, linkUrl, target);
        }

        #endregion

        #region Method Tests

        [TestMethod]
        public void GetControlTest()
        {
            int pageLinkKey = 2;
            string displayText = "display";
            string linkUrl = "url";
            string target = null;
            WikiPageLink pageLinkTarget = new WikiPageLink(pageLinkKey, displayText, linkUrl, target);
            Control control = pageLinkTarget.GetControl();
            Assert.IsInstanceOfType(control, typeof(HyperLink));
            HyperLink link = control as HyperLink;
            Assert.AreEqual(displayText, link.Text);
            Assert.AreEqual(linkUrl, link.NavigateUrl);
            Assert.AreEqual(string.Empty, link.Target);
        }

        #endregion

        #region Equality Tests

        [TestMethod]
        public void EqualsObjectTest()
        {
            object target = new WikiPageLink(1, "display", "url", "target");
            object other = new WikiPageLink(1, "display", "url", "target");
            Assert.IsTrue(target.Equals(other));
        }

        [TestMethod]
        public void EqualsObjectNullTest()
        {
            object target = new WikiPageLink(1, "display", "url", "target");
            object other = null;
            Assert.IsFalse(target.Equals(other));
        }

        [TestMethod]
        public void EqualsWikiPageLinkTargetNullTest()
        {
            WikiPageLink target = new WikiPageLink(1, "display", "url", null);
            WikiPageLink other = new WikiPageLink(1, "display", "url", null);
            Assert.IsTrue(target.Equals(other));
        }

        [TestMethod]
        public void EqualsWikiPageLinkTargetNullInequalTest()
        {
            WikiPageLink target = new WikiPageLink(1, "display", "url", null);
            WikiPageLink other = new WikiPageLink(1, "display", "url", "target");
            Assert.IsFalse(target.Equals(other));
        }

        #endregion
    }
}
